package com.fire.control.system.module.system.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.config.redis.RedisService;
import com.fire.control.system.entity.Resource;
import com.fire.control.system.module.system.dao.ResourceMapper;
import com.fire.control.system.module.system.service.ResourceService;
import com.fire.control.system.statics.Key;

@Service("resourceService")
public class ResourceServiceImpl implements ResourceService{

	@Autowired private ResourceMapper resourceMapper;
	@Autowired private RedisService redisService;
	
	public Resource getResourceByUrl(String url){
		return resourceMapper.getResourceByUrl(url);
	}
	
	public List<Resource> getResourcesByuid(String uid){
		String key = Key.redis_key_lora_resources_user_id_+uid;
		List<Resource> resources = redisService.getList(key, Resource.class);
		if(resources==null || resources.size()<1){
			resources = resourceMapper.getResourcesByuid(uid);
			redisService.setList(key, resources,Key.redis_key_lora_resources_user_id_.getExpire());
		}
		return resources;
	}
	
	public String getResourcesUrlByuid(String uid){
		String key = Key.redis_key_lora_resources_web_urls_user_id_+uid;
		String urls = redisService.get(key);
		if(StringUtils.isEmpty(urls)){
			urls = resourceMapper.getResourcesUrlByuid(uid);
			redisService.set(key, urls,Key.redis_key_lora_resources_web_urls_user_id_.getExpire());
		}
		return urls;
	}
	
	public boolean getResourcesApp(String uid){
		String key = Key.redis_key_lora_resources_app_urls_user_id_+uid;
		boolean b = false; 
		String urls = redisService.get(key);
		if(StringUtils.isEmpty(urls)){
			b = resourceMapper.getResourcesApp(uid);
			redisService.set(key, String.valueOf(b),Key.redis_key_lora_resources_app_urls_user_id_.getExpire());
		}else{
			b = Boolean.valueOf(urls);
		}
		return b;
	}
	
}
